﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class product_saving_admin_saving_edit : System.Web.UI.Page
{
    SavingDetailsBLL bll = new SavingDetailsBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        //((Admin)this.Master).mainCategory = "ACCOUNTS";

        if (IsPostBack == false)
        {

            String id = (Request.QueryString["custID"]);
            bll.custID = id;
            this.hfCustId.Value = id;
            DataSet self = bll.getSelfDetail();
            this.Self_FormView.DataSource = bll.getSelfDetail();
            this.Self_FormView.DataBind();


            /*this.tbfName.Text = Session["fName"] as String;
            this.tblName.Text = Session["lName"] as String;
            this.tbIC.Text = Session["custNRIC"] as String;
            this.tbNationality.Text = Session["custNationality"] as String;
            this.tbGender.Text = Session["custGender"] as String;
            this.tbDob.Text = Session["custDob"] as String;
            this.tbMobile.Text = Session["custMobile"] as String;
            this.tbEmail.Text = Session["custEmail"] as String;
            this.tbAddress.Text = Session["custAddress"] as String;
            this.tbPostalCode.Text = Session["custPostal"] as String;
            this.tbOccupation.Text = Session["custOccupation"] as String;
            this.tbStatus.Text = Session["custMarital"] as String;
            this.tbHome.Text = Session["custTel"] as String;*/
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string status = (this.Self_FormView.FindControl("tbStatus") as TextBox).Text;
        string occupation = (this.Self_FormView.FindControl("tbOccupation") as TextBox).Text; //tbOccupation.Text;
        string home = (this.Self_FormView.FindControl("tbHome") as TextBox).Text;// tbHome.Text;
        string mobile = (this.Self_FormView.FindControl("tbMobile") as TextBox).Text;// tbMobile.Text;
        string email = (this.Self_FormView.FindControl("tbEmail") as TextBox).Text;// tbEmail.Text;
        string address = (this.Self_FormView.FindControl("tbAddress") as TextBox).Text;// tbAddress.Text;
        string postal = (this.Self_FormView.FindControl("tbPostalCode") as TextBox).Text;// tbPostalCode.Text;
        string fname = (this.Self_FormView.FindControl("tbfName") as TextBox).Text;// tbfName.Text;
        string lname = (this.Self_FormView.FindControl("tblName") as TextBox).Text;// tblName.Text;
        string dob = (this.Self_FormView.FindControl("tbDob") as TextBox).Text;// tbDob.Text;
        string nationality = (this.Self_FormView.FindControl("tbNationality") as TextBox).Text;// tbNationality.Text;
        string nric = (this.Self_FormView.FindControl("tbIC") as TextBox).Text;// tbIC.Text;

        string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(strConnectionString);
       

        try
        {
        myConnection = DBManager.getSqlConnection();
        SqlCommand myCommandd = new SqlCommand("UPDATE CUSTOMER SET fName = @fname, lName = @lname, custDob =@DOB, custNationality=@nationality, custNRIC=@nric, custMobile=@mobile, custTel = @home, email=@email, custAddress=@address, custPostal=@postal, custOccupation=@occupation, custMarital=@status WHERE custID = '" +hfCustId.Value+"'", myConnection);
        myConnection.Open();
        myCommandd.Parameters.AddWithValue("@status", status);
        myCommandd.Parameters.AddWithValue("@occupation", occupation);
        myCommandd.Parameters.AddWithValue("@home", home);
        myCommandd.Parameters.AddWithValue("@mobile", mobile);
        myCommandd.Parameters.AddWithValue("@email", email);
        myCommandd.Parameters.AddWithValue("@address", address);
        myCommandd.Parameters.AddWithValue("@postal", postal);
        myCommandd.Parameters.AddWithValue("@fname", fname);
        myCommandd.Parameters.AddWithValue("@lname", lname);
        myCommandd.Parameters.AddWithValue("@DOB", Util.StringToDate(dob));
        myCommandd.Parameters.AddWithValue("@nationality", nationality);
        myCommandd.Parameters.AddWithValue("@nric", nric);
        myCommandd.ExecuteNonQuery();

        Label1.Text = "Customer's  information has been updated. Thank you.";

        myConnection.Close();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
        Response.Write(ex);
        }
        finally
        {
        myConnection.Close();
        }

        /*
        string sql = "select C.custID, C.fName,C.lName,S.accountNo,C.custGender,C.custNRIC,C.custNationality,C.custDob,C.custTel,C.custMobile,C.custAddress,C.email,C.custOccupation,C.custPostal,C.custMarital" +

                                            " From CUSTOMER C INNER JOIN SAVING_ACCOUNT S ON C.custID = S.custID Where C.custID='" + hfCustId.Value + "'";
        
        SqlCommand cmd = new SqlCommand(sql, myConnection);
        myConnection.Open();
        SqlDataReader dr = cmd.ExecuteReader();

       
        if (dr.Read())
        {
            Session["userName"] = dr.GetString(1) + dr.GetString(2);
            Session["userId"] = dr.GetString(0);
            Session["fName"] = dr.GetString(1);
            Session["lName"] = dr.GetString(2);
            Session["savingAccountNo"] = dr.GetString(3);
            Session["custGender"] = dr.GetString(4);
            Session["custNRIC"] = dr.GetString(5);
            Session["custNationality"] = dr.GetString(6);
            Session["custDob"] = dr.GetDateTime(7);
            Session["custTel"] = dr.GetString(8);
            Session["custMobile"] = dr.GetString(9);
            Session["custAddress"] = dr.GetString(10);
            Session["custEmail"] = dr.GetString(11);
            Session["custOccupation"] = dr.GetString(12);
            Session["custPostal"] = dr.GetString(13);
            Session["custMarital"] = dr.GetString(14);  

        }
        dr.Close();*/

    }
   
}